#include<bits/stdc++.h>
using namespace std;

typedef long long i64;
const i64 inf = 0x3f3f3f3f3f3f3f3f;
i64 T, n;
i64 a[200005];
i64 dp[200005][2];
i64 sum[200005];
map <i64, i64> m;
i64 pre[200005];

int main(){
    freopen("color.in", "r", stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> T;
    while( T-- ){
        cin >> n;
        for(i64 i=1;i<=n;++i) cin >> a[i];
        m.clear();
        for(i64 i=2;i<=n;++i){
            sum[i] = sum[i-1];
            if(a[i] == a[i-1]) sum[i] += a[i];
            dp[i][0] = dp[i][1] = 0;
        }
        for(i64 i=1;i<=n;++i){
            pre[i] = 0;
            if(!m.count(a[i])){
                m[a[i]] = i;
            }else{
                pre[i] = m[a[i]];
                m[a[i]] = i;
            }
        }
        
        i64 ans = sum[n];
        for(i64 i=2;i<=n;++i){
            if(pre[i])
            for(i64 j = max(1LL, pre[i] - 5);j<min(i-2, pre[i] + 5);++j){
                for(i64 k=0;k<=1;++k){
                    for(i64 p=0;p<=1;++p){
                        for(i64 h=0;h<=1;++h){
                            i64 val = 0;
                            val += dp[j][k];
                            if(h==0){
                                if(a[j-(k^1)] == a[i-(p^1)]) val += a[i-(p^1)];
                                val += sum[i-2] - sum[j+1];
                                if(a[j-k] == a[j+1]) val += a[j+1];
                                if(a[i-p] == a[i-2]) val += a[i-2];
                            }else{
                                if(a[j-k] == a[i-p]) val += a[i-p];
                                val += sum[i-2] - sum[j+1];
                                if(a[j-(k^1)] == a[j+1]) val += a[j+1];
                                if(a[i-(p^1)] == a[i-2]) val += a[i-2];
                            }
                            dp[i][p] = max(dp[i][p], val);
                        }
                    }
                }
            }
            if(pre[i-1])
            for(i64 j = max(1LL, pre[i-1] - 5);j<min(i-2, pre[i-1] + 5);++j){
                for(i64 k=0;k<=1;++k){
                    for(i64 p=0;p<=1;++p){
                        for(i64 h=0;h<=1;++h){
                            i64 val = 0;
                            val += dp[j][k];
                            if(h==0){
                                if(a[j-(k^1)] == a[i-(p^1)]) val += a[i-(p^1)];
                                val += sum[i-2] - sum[j+1];
                                if(a[j-k] == a[j+1]) val += a[j+1];
                                if(a[i-p] == a[i-2]) val += a[i-2];
                            }else{
                                if(a[j-k] == a[i-p]) val += a[i-p];
                                val += sum[i-2] - sum[j+1];
                                if(a[j-(k^1)] == a[j+1]) val += a[j+1];
                                if(a[i-(p^1)] == a[i-2]) val += a[i-2];
                            }
                            dp[i][p] = max(dp[i][p], val);
                        }
                    }
                }
            }
            if(i >= 4)
            for(i64 k=0;k<=1;++k){
                for(i64 p=0;p<=1;++p){
                    dp[i][p] = max(dp[i][p], dp[i-2][k] + (a[i-p]==a[i-2-k]?a[i-p]:0) + (a[i-(p^1)]==a[i-2-(k^1)]?a[i-(p^1)]:0));
                }
            }
            for(i64 p=0;p<=1;++p){
                dp[i][p] = max(dp[i][p], sum[i-2] + (a[i-1]==a[i-2]?a[i-1]:0));
                dp[i][p] = max(dp[i][p], sum[i-2] + (a[i]  ==a[i-2]?a[i]  :0));
            }
            for(i64 p=0;p<=1;++p){
                ans = max(ans, dp[i][p] + ((i+1<=n)?(sum[n] - sum[i+1]):0) + (a[i-1]==a[i+1]?a[i+1]:0));
                ans = max(ans, dp[i][p] + ((i+1<=n)?(sum[n] - sum[i+1]):0) + (a[i]  ==a[i+1]?a[i]  :0));
            }
        }
        cout << ans << '\n';
    }


    return 0;
}